<template>
  <div id="app">
    <el-form ref="form" :model="form" label-width="80px" :rules="addForm">
      <h1>工程设计变更通知单</h1>
      <el-form-item label="工程名称" class="head" prop="project_name">
        <el-input v-model="form.project_name"></el-input>
      </el-form-item>
      <el-form-item label="项目编号" class="head" prop="project_number">
        <el-input v-model="form.project_number"></el-input>
      </el-form-item>
      <el-form-item label="承包单位" class="head" prop="contractor_unit">
        <el-input v-model="form.contractor_unit"></el-input>
      </el-form-item>
      <el-form-item label="表号" class="head" prop="id">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="设计方" prop="designer">
        <el-input  v-model="form.designer" style="width:300px"></el-input>
      </el-form-item>
      <el-form-item label="变更项目增加内容" prop="change_add">
        <el-input type="textarea" v-model="form.change_add" style="width:680px" placeholder="若没有增加则填无"></el-input>
      </el-form-item>
      <el-form-item label="变更项目核减内容" prop="change_del">
        <el-input type="textarea" v-model="form.change_del" style="width:680px" placeholder="若没有减少则填无"></el-input>
      </el-form-item>
      <el-form-item label="通知日期" prop="date">
        <el-date-picker type="date" placeholder="选择日期" v-model="form.date" style="width: 100%;"></el-date-picker> 
      </el-form-item>
      <el-form-item label="监理姓名" prop="name">
        <el-input  v-model="form.name" style="width:300px"></el-input>
      </el-form-item>
      <el-form-item label="上传附件">
          <el-upload
            class="upload-demo"
            ref="upload"
            action="#"
            :limit="3"
            :http-request="uploadOk"
            :auto-upload="true">
            选取文件
          </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button @click.native="submit" style="width: 100px;" type="primary">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
      return {
        form: {
          project_name: '',
          project_number: '',
          contractor_unit: '',
          id: '',
          designer: '',
          change_add: '',
          change_del: '',
          date: '',
          name: '',
          status:0
        },
        addForm: {
          project_name: [{ required: true, message: '工程名称不能为空!', trigger: 'blur' },],
          project_number: [{ required: true, message: '项目编号不能为空!', trigger: 'blur' },],
          contractor_unit: [{ required: true, message: '承包单位不能为空!', trigger: 'blur' },],
          id: [{ required: true, message: '表号不能为空!', trigger: 'blur' },],
          designer: [{ required: true, message: '设计方不能为空!', trigger: 'blur' },],
          date: [{ required: true, message: '编制日期不能为空!', trigger: 'blur' },],
          name: [{ required: true, message: '监理姓名不能为空!', trigger: 'blur' },],
        },
      }
    },
    methods: {
      // 上传文件
      uploadOk (val) {
        const fd = new FormData()
        fd.append('file', val.file) // 'file是与后端接收对应的'
        this.$axios({
          method: 'post',
          url: 'http://112.124.46.193:8080/grade_protection/sysInfo/annex/',
          headers: { 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryXUIhYHlAmsiYAKUG' },
          data: fd
        })
          .then(response => {
            // if (response.data.meta.status === 201) {
            //   this.$message.success(response.data.meta.msg)
            // } else {
            //   this.$message.error('文件上传失败！')
            // }
          })
          .catch(() => {
            this.$message.error('请先提交信息！')
          })
      },
      submit(){
        if(this.form.change_add == "" && this.form.change_del == ""){
            this.$message({
              message: '变更项目增加内容或变更项目核减内容至少一个不能为空',
              type: 'error',
              center: true
          });
        } else {
          //   这样做的缺陷是一次只能新建一个文件
          sessionStorage.setItem("notification", JSON.stringify(this.form));
          this.$message({
            message: '提交成功',
            type: 'success',
            center: true
        });
        }
      }
    }
}
</script>

<style  lang="less" scoped>
#app{
  height: auto;
}
.el-form {
  width: 800px;
  height: auto;
  margin: 0 auto;
  border: 1px dashed rgba(0, 50, 100, 0.8);
  position: relative;
  h1{
    text-align: center;
  }
  .head{
    .el-input {
      width: 300px;
    }
    display: inline-block;
  }
}
.el-upload--text{
  width: 419px;
  height: 36px;
}
</style>